﻿using System.IO;

namespace GeneralDFS.Common.IO
{
    public class SpaceHelper
    {
        /// <summary>
        /// 获取磁盘的可用大小
        /// </summary>
        /// <param name="path">可以是磁盘名，也可以是文件夹路劲</param>
        /// <returns>返回磁盘大小</returns>
        public static long GetDriveAvailableSpace(string path)
        {
            string driveName = Directory.GetDirectoryRoot(path);
            DriveInfo driveInfo = new DriveInfo(driveName);
            return driveInfo.AvailableFreeSpace;
        }
        /// <summary>
        /// 获取文件夹的使用空间
        /// </summary>
        /// <param name="directoryPath">目录地址</param>
        /// <returns>返回文件夹的大小</returns>
        public static long GetDirectoryUsedSpace(string directoryPath)
        {
            long len = 0;
            if (Directory.Exists(directoryPath))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
                //获取当前目录下所有文件的大小
                foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                {
                    len += fileInfo.Length;
                }

                DirectoryInfo[] cihldDirectorys = directoryInfo.GetDirectories();

                //获取当前目录下所有文件夹的大小
                for (int i = 0; i < cihldDirectorys.Length; i++)
                {
                    len += GetDirectoryUsedSpace(cihldDirectorys[i].FullName);
                }
            }
            return len;
        }
    }
}
